import { _decorator, Animation, Color, Component, Node, Sprite, toDegree, toRadian, tween, Tween, Vec3 } from 'cc';
import { Blade } from './Blade';
import { Core } from '../core/Core';
import { EGameEvent, EprefabName } from '../core/Config';
import { Context } from './Context';
import { PlayerWeapon } from './PlayerWeapon';
import { NumberText } from './NumberText';
const { ccclass, property } = _decorator;

export enum EplayerAnimation {
    IDLE,
    WALK,
}
/**
 * 玩家
 */
@ccclass('Player')
export class Player extends Component {

    @property(Node)
    animation: Node;

    @property(Node)
    blade: Node;

    private _animation: Animation;
    private _animationSprite: Sprite;


    speed: number = 3;
    direction: Vec3 = new Vec3();
    isMoving: boolean = false;

    life: number = 0;

    protected onLoad(): void {
        this._animation = this.animation.getComponent(Animation);
        this._animationSprite = this.animation.getComponent(Sprite);
    }
    protected onEnable(): void {
        this.blade.active = false;
        this.life=Core.instance.playerData.hp;
    }
    protected onDisable(): void {
        
    }
    start() {

    }

    update(deltaTime: number) {
        if (this.isMoving) {
            this.node.setPosition(
                this.node.position.x + this.speed * this.direction.x,
                this.node.position.y + this.speed * this.direction.y,
            );
            if (this.direction.x >= 0) {
                this.animation.setScale(1, 1);
            } else {
                this.animation.setScale(-1, 1);
            }
        }
    }

    hurt(value: number) {
        this.life-=value;
        if (this.life > 0) {
            //受到伤害
            Core.instance.event.emit(EGameEvent.PLAYER_HURT,value);
            this.slashOnce();
            NumberText.show(`${Math.ceil(value)}`, this.node.worldPosition, Color.RED);
        }else {
            //死亡
            this.life = 0;
            Core.instance.event.emit(EGameEvent.PLAYER_DEAD);
        }
    }

    private slashOnce() {
        this._animationSprite.color = Color.RED;
        this.scheduleOnce(() =>this._animationSprite.color=Color.WHITE, 0.15);
    }

    setAnimation(animation: EplayerAnimation) {
        switch (animation) {
            case EplayerAnimation.IDLE:
                this._animation.play("PlayerIdle");
                this.isMoving = false;
                break;
            case EplayerAnimation.WALK:
                this._animation.play("PlayerWalk");
                this.isMoving = true;
                break;
            default:
                break;
        }
    }
    /**
     * 普通攻击
     */
    attack() {
        if (!this.blade.active) {
            this.blade.active = true;
            this.blade.getComponent(Blade).show();
        }
    }
    /**
     * 法术攻击:释放能量球
     */
    castEnergyBall() {
        let angle = 0;
        for (let i = 0; i < 32; i++) {
            this.scheduleOnce(() => {
                angle = i * 22.5;
                let node = Core.instance.creatNode(EprefabName.ENERGY_BALL, Context.weapons);
                node.angle = angle;//toDegree(Math.atan2(this.direction.y, this.direction.x));
                node.setWorldPosition(this.node.getWorldPosition());

                let energyBall = node.getComponent(PlayerWeapon);
                energyBall.direction.set(Math.cos(toRadian(angle)), Math.sin(toRadian(angle)));
                energyBall.speed = 7;
                energyBall.isMoving = true;
            }, i * 0.05);
        }
    }
    /**
     * 闪现
     */
    castSlash() {
        Tween.stopAllByTarget(this.node);
        let distance = 200;
        tween(this.node)
            .by(0.2, { position: new Vec3(this.direction.x * distance, this.direction.y * distance, 0) }, { easing: 'smooth' })
            .start();
    }

    /**
     * 射箭
     */
    castArrow() {
        let currAngle = toDegree(Math.atan2(this.direction.y, this.direction.x));
        let count = 9;
        let startAngle = currAngle - Math.floor(count) * 8;
        for (let i = 0; i < count; i++) {
            let angle = startAngle + i * 8;
            let node = Core.instance.creatNode(EprefabName.ARROW, Context.weapons);
            node.angle = angle;
            node.setWorldPosition(this.node.worldPosition);

            let arrow = node.getComponent(PlayerWeapon);
            arrow.direction.set(Math.cos(toRadian(angle)), Math.sin(toRadian(angle)));
            arrow.speed = 5;
            arrow.isMoving = true;
        }
    }
}


